<template>
	<view class="page">
		<view class="user-setting">
			<view class="user-info mt30">
				<view class="headwear">
					<image class="avatar" :src="userData.headImgUrl" mode="" @click="goEdit()">
					</image>
				</view>
			</view>

			<view class="order-form mt20">
				<u--form labelPosition="left" :model="userData" ref="form">
					<u-form-item labelWidth="80" label="昵称" prop="nickname" borderBottom ref="item">
						<u--input v-model="userData.nickname" border="none" placeholder="昵称"></u--input>
					</u-form-item>

					<u-form-item labelWidth="80" label="会员号" prop="userName" borderBottom ref="item">
						<u--input :disabled='true' v-model="userData.userName" placeholder="xdl0001" border="none">
						</u--input>
					</u-form-item>

					<u-form-item labelWidth="80" label="会员等级" prop="level" borderBottom ref="item">
						<u--input :disabled='true' v-model="userData.chLevel" placeholder="VIP会员" border="none">
						</u--input>
					</u-form-item>


					<u-form-item labelWidth="80" label="性别" prop="sex" borderBottom ref="item">
						<u-radio-group v-model="userData.sex">
							<u-radio activeColor="#ac4040" :customStyle="{marginRight: '10px'}" :label="'男'" :name="1">
							</u-radio>

							<u-radio activeColor="#ac4040" :customStyle="{marginRight: '10px'}" :label="'女'" :name="2">
							</u-radio>
						</u-radio-group>
					</u-form-item>


					<u-form-item @click="showCalendar = true; hideKeyboard()" labelWidth="80" label="生日" prop="birthday"
						borderBottom ref="item">
						<u--input v-model="userData.birthday" disabled disabledColor="#ffffff" placeholder="生日"
							border="none"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>

					<u-form-item @click="selectTag()" labelWidth="80" label="喜欢" prop="interest" borderBottom
						ref="item">

						<view class="tag-group">
							<view class="tag-item" v-for="(item, index) in likes" :key="index">
								{{item.navName}}
							</view>

						</view>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
				</u--form>
			</view>
		</view>
		<view style="margin: 20rpx;font-size: 30rpx;font-weight: bold;color: #ac4040;">实名认证</view>
		<view class="real-name">
			<u--form labelPosition="left" :model="userData" ref="real-form">
				<u-form-item labelWidth="80" label="姓名" prop="realName" borderBottom ref="item">
					<u--input v-model="userData.realName" border="none" placeholder="姓名"></u--input>
				</u-form-item>

				<u-form-item labelWidth="80" label="手机号" prop="phone" borderBottom ref="item">
					<u--input v-model="userData.phone" border="none" placeholder="手机号"></u--input>
					<u-button slot="right" shape="circle" openType="getPhoneNumber" @getphonenumber="getPhoneNumber"
						color="#ac4040" size="small" icon="phone" iconColor="#ffffff">获取手机号</u-button>

				</u-form-item>

				<u-form-item labelWidth="80" label="身份证" prop="idCard" borderBottom ref="item">
					<u--input v-model="userData.idCard" border="none" placeholder="身份证"></u--input>
				</u-form-item>
			</u--form>
		</view>

		<view style="font-size: 20rpx;color: #909399;margin: 20rpx;">
			<text>点击“确定”则表示您已详细阅读统一并接受</text>
			<text style="color: #ac4040;" @click="goUrl('secrecy')">《隐私权政策》</text>
			<text style="color: #ac4040;" @click="goUrl('disclaimer')">《免责协议》</text>
		</view>

		<view class="sub-btn" @click="subBtn()">
			确认
		</view>

		<u-datetime-picker :show="showCalendar" v-model="dataValue" cancelColor="#ac4040" confirmColor="#ac4040"
			minDate="-2209017600000" maxDate="4070880000000" mode="date" closeOnClickOverlay @confirm="confirm"
			@cancel="cancel" color="#ac4040" @close="close">
		</u-datetime-picker>



		<u-popup :safeAreaInsetBottom="true" :safeAreaInsetTop="false" mode="center" :show="showTag" round="10"
			:overlay="true" borderRadius="2" :closeable="true" :closeOnClickOverlay="false" @close="closeTag">

			<view style="font-size: 30rpx;text-align: center;line-height: 66rpx;">喜欢</view>
			<view class="popup-slot">

				<view class="u-page__tag-item" v-for="(item, index) in tabs" :key="index">
					<view>
						<u-tag :text="item.navName" :plain="!item.checked" type="primary" :name="item.cid"
							@click="checkboxClick">
						</u-tag>
					</view>
				</view>

			</view>

		</u-popup>


	</view>
</template>

<script>
	import cache from '@/common/cache.js'
	export default {
		data() {
			const d = new Date()
			const year = d.getFullYear()
			let month = d.getMonth() + 1
			month = month < 10 ? `0${month}` : month
			const date = d.getDate()
			return {
				dataValue: Number(new Date()),
				userData: {},
				close1: true,
				showCalendar: false,
				showTag: false,
				likes: [],
				tabs: []
			}
		},
		onLoad() {
			this.isLogin();
			this.loadTabs()
		},
		methods: {
			loadTabs() {
				this.$Request.get(this.$api.activity.getActClassify).then(
					res => {
						const dataList = res.data;
						console.log('dataList', dataList)
						if (dataList.length > 0) {
							for (var i = 0; i < dataList.length; i++) {
								const item = dataList[i];
								const cid = []
								cid = this.userData.interest ? this.userData.interest.split(',') : [];
								if (cid.length > 0) {
									for (var j = 0; j < cid.length; j++) {
										if (item.cid == cid[j]) {
											item.checked = true;

											this.likes.push(item);
										}
									}
								}
								if (item.cid != 1) {
									this.tabs.push(item);
								}

							}
						}
					})
			},
			checkboxClick(cid) {
				for (var i = 0; i < this.tabs.length; i++) {
					if (this.tabs[i].cid == cid) {
						this.tabs[i].checked = !this.tabs[i].checked;
						if (this.tabs[i].checked) {
							this.likes.push(this.tabs[i])
						} else {
							var index = this.likes.findIndex(item => {
								if (item.cid == this.tabs[i].cid) {
									return true
								}
							});
							this.$delete(this.likes, index);
						}
					}
				}



				// console.log('name', name);
				// this.tabs[name].checked = !this.tabs[name].checked
				// if (this.tabs[name].checked) {
				// 	//likes
				// 	this.likes.push(this.tabs[name])
				// } else {
				// 	this.$delete(this.likes, name)
				// }
			},
			isLogin() {
				const that = this;
				//const value = uni.getStorageSync('setUserData');
				const value = cache.get('setUserData', false);
				console.log('value', value)
				if (!value) {
					uni.$u.toast("用户未登陆！");
					//this.login();
					uni.reLaunch({
						url: '/pages/my/index',
					});
				} else {
					this.userData = value;
				}

			},
			getPhoneNumber(e) {
				uni.login({
					success: (res) => {
						console.log("login", JSON.stringify(res));
						const params = {
							"code": res.code,
							"iv": e.detail.iv,
							"encryptedData": e.detail.encryptedData
						}
						this.$Request.post(this.$api.user.getPhoneNumber, params).then(
							res => {
								if (res.code == 20000) {
									uni.$u.toast("获取成功");
									this.userData.phone = res.data.phoneNumber;
								} else {
									uni.$u.toast("获取失败，请重新尝试");
								}
							})


					}
				})

			},
			login() {
				uni.getUserProfile({
					desc: '登录',
					success: (res) => {
						uni.login({
							success: (v) => {
								const params = {
									"code": v.code,
									"nickname": res.userInfo.nickName,
									"headImgUrl": res.userInfo.avatarUrl
								}
								this.$Request.post(this.$api.user.loginWithWechat, params).then(
									res => {
										if (res.code == 20000) {
											cache.put('token', res.data.token, 24 * 60 * 60 * 90)
											cache.put('setUserData', res.data.userinfo, 24 *
												60 * 60 * 90)
											uni.showToast({
												icon: 'success',
												position: 'bottom',
												title: '登录成功'
											});
										} else {
											uni.showToast({
												icon: 'error',
												position: 'bottom',
												title: res.data.message
											});
										}
										uni.hideLoading()
									})
							}
						})
					},
					fail: (res) => {
						console.log(res)
					}
				});
			},
			subBtn() {
				let interest = []
				this.likes.forEach(i => {
					interest.push(i.cid);
				})
				this.userData.interest = interest + "";
				console.log("sub", this.userData)
				this.$Request.post(this.$api.user.editUser, this.userData).then(
					res => {
						if (res.code === 20000) {
							cache.put('setUserData', res.data, 24 * 60 * 60); //存入缓存
							uni.$u.toast("用户信息更新成功！");
						}
					})
			},
			goUrl(e) {
				uni.navigateTo({
					url: "/pages/my/components/privacy?type=" + e
				})
			},
			hideKeyboard() {
				uni.hideKeyboard()
			},
			selectTag() {
				this.showTag = true
			},
			closeTag() {
				this.showTag = false;
			},
			confirm(e) {
				console.log(e);
				this.showCalendar = false
				this.userData.birthday = this.result(e.value);
			},
			close() {
				this.showCalendar = false
			},
			cancel() {
				this.showCalendar = false;
			},
			result(time) {
				const timeFormat = uni.$u.timeFormat;
				return timeFormat(time, 'yyyy-mm-dd')
			},

		}
	}
</script>

<style lang="scss">
	.u-page__tag-item {
		margin: 10px;
	}

	.popup-slot {
		width: 300px;
		flex-flow: wrap;
		@include flex;
		//justify-content: center;
		//align-items: center;
	}

	.sub-btn {
		color: #ffffff;
		background-color: $xdl-color-primary;
		border-radius: 50rpx;
		width: 500rpx;
		height: 80rpx;
		display: flex;
		margin: 80rpx auto;
		align-items: center;
		justify-content: center;
	}

	.real-name {
		background-color: #ffffff;
		margin: 20rpx 20rpx;
		padding: 20rpx;
		border-radius: 18rpx;
	}

	.tag-group {
		font-size: 20rpx;
		display: flex;
		color: #ffffff;

		.tag-item {
			border-radius: 8rpx;
			padding: 5rpx;
			text-align: center;
			//width: 50rpx;
			margin: 0 8rpx;
			background-color: $xdl-color-primary;
		}
	}

	.order-form {

		font-size: 24rpx;
	}

	.user-setting {
		background-color: #ffffff;
		margin: 20rpx 20rpx;
		padding: 20rpx;
		border-radius: 18rpx;
	}

	.user-info {
		z-index: 20;
		position: relative;
		display: flex;
		justify-content: center;

		.headwear {
			display: flex;
			align-items: flex-end;
			width: 125rpx;
			height: 138rpx;
			padding: 0 4rpx 4rpx;
			background: url("") center/cover no-repeat;
		}



		.avatar {
			display: block;
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			margin: 0 auto;
		}
	}
</style>
